﻿var SinaWeiBo = {
	_oauth: {}
};

SinaWeiBo.REQUEST_TOKEN_URL = 'http://api.t.sina.com.cn/oauth/request_token';
SinaWeiBo.AUTHORIZE_URL = 'http://api.t.sina.com.cn/oauth/authorize';
SinaWeiBo.ACCESS_TOKEN_URL = 'http://api.t.sina.com.cn/oauth/access_token';
SinaWeiBo.CONSUMER_KEY = "2389617439";
SinaWeiBo.CONSUMER_SECRET = "d92fef9074e9a509187c9c6271e0e1e8";

SinaWeiBo.UPDATE_API_URL = "http://api.t.sina.com.cn/statuses/update.json";

SinaWeiBo.username = "iamdanciben@gmail.com";
SinaWeiBo.password = "iamdanciben";

SinaWeiBo.oauth_init = function() {
	SinaWeiBo._oauth = ChromeExOAuth.initBackgroundPage({
	  'request_url': SinaWeiBo.REQUEST_TOKEN_URL,
	  'authorize_url': SinaWeiBo.AUTHORIZE_URL,
	  'access_url': SinaWeiBo.ACCESS_TOKEN_URL,
	  'consumer_key': SinaWeiBo.CONSUMER_KEY,
	  'consumer_secret': SinaWeiBo.CONSUMER_SECRET
	});
};

SinaWeiBo.post_word = function(word) {
	function callback(resp, xhr) {
		console.log("post word successfully");
	};
	
	SinaWeiBo._oauth.authorize(function() {
		console.log("on authorize");
		var url = SinaWeiBo.UPDATE_API_URL;
		SinaWeiBo._oauth.sendSignedRequest(url, callback, {
			'method': 'POST',
			'headers': {
				'Content-Type': 'application/x-www-form-urlencoded',
				'Content-length': word.length,
				"Connection": "close"
			},
			'parameters': {
				'alt': 'json',
				'status': word,
﻿				'userId': SinaWeiBo.username,
				'passwd': SinaWeiBo.password
			}
		});
	});
};